﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Linq.Expressions;
using XYAC.Framework.MonitorTireMaterialManual.Extensions;
using XYAC.Framework.MonitorTireMaterialManual.IServices;
using XYAC.Framework.MonitorTireMaterialManual.OracleRemoteDB;

namespace XYAC.Framework.MonitorTireMaterialManual.Services
{
    public class YB_LOT_INSPECTService : ServiceBase<int, OracleMesContext>, IYB_LOT_INSPECTService
    {
        private const string __REMOTE_SYNC_STATE_PENDING = "PENDING";
        private const string __REMOTE_SYNC_STATE_COMPLETE = "COMPLETE";

        public YB_LOT_INSPECTService(IOptionsMonitor<AppConfig> appConfig, ILogger<YB_LOT_INSPECTService> logger, IServiceProvider provider) : base(appConfig, logger, provider)
        {
            //this.Db = provider.GetService<OracleMesContext>();
            this.Logger = logger;
        }

        //public OracleMesContext Db { get; }
        public ILogger Logger { get; }

        /// <summary>
        /// 返回当前状态的PlanNo和Sn数据,获取最新条码数据
        /// </summary>
        /// <param name="machineNoList"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public IEnumerable<YB_LOT_INSPECT> GetSnData(params string[] machineNoList)
        {
            using (var scope = this.Provider.CreateScope())
            {
                var Db = scope.ServiceProvider.GetService<OracleMesContext>();
                var snList = Db.YB_LOT_INSPECT.Where(T => machineNoList.Contains(T.EQP) && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_PENDING).ToList();
                return snList;
            }
        }

        public IEnumerable<YB_LOT_INSPECT> GetSnData(DateTime plandate, params string[] machineNoList)
        {
            using (var scope = this.Provider.CreateScope())
            {
                var Db = scope.ServiceProvider.GetService<OracleMesContext>();
                var snList = Db.YB_LOT_INSPECT.Where(T => machineNoList.Contains(T.EQP) && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_PENDING && T.PLAN_DATE.Value.Date == plandate).ToList();
                return snList;
            }
        }

        public IEnumerable<YB_LOT_INSPECT> GetSnPage(int pageIndex = 0, int rowCount = 50, Expression<Func<YB_LOT_INSPECT, bool>> filterCondition = null, Expression<Func<YB_LOT_INSPECT, object>> orderByCondition = null, Expression<Func<YB_LOT_INSPECT, object>> orderByDescCondition = null)
        {
            using (var scope = this.Provider.CreateScope())
            {
                var Db = scope.ServiceProvider.GetService<OracleMesContext>();
                IQueryable<YB_LOT_INSPECT> data = Db.Set<YB_LOT_INSPECT>();
                if (filterCondition != null)
                    data = data.Where(filterCondition);
                if (orderByCondition != null)
                    data = data.OrderBy(orderByCondition);

                if (orderByDescCondition != null)
                    data = data.OrderBy(orderByDescCondition);
                return data.ToList();
            }
        }
    }
}
